package view.ui
{
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	[Embed(source="../../../lib/TimeSaver.swf", symbol="classes.UI")]
	/**
	 * ...
	 * @author 
	 */
	public class UI extends MovieClip 
	{
		
		public  var life:MovieClip;
		private var maxLife:int = 20;
		private var curLife:int;
		public  var power:MovieClip;
		private var maxPower:int = 20;
		private var curPower:int;
		public  var txtMoney:TextField;
		private var money:int;
		public  var txtHitCount:TextField;
		public  var btn:MovieClip;
		public  var tool:MovieClip;
		private var hitCount:int;
		private var powerCount:int;
		
		public  var bKouxue:Boolean;
		public  var bAddPower:Boolean;
		private var bMenuClick:Boolean;
		
		public function UI() 
		{
			init();
			initListener();
		}
		//从swf中读取相关的子对象，并附给对象中的响应对象，再初始化
		private function init():void {
			//初始化UI类的属性
			life 		= new MovieClip();
			power		= new MovieClip();
			txtMoney 	= new TextField();
			btn         = new MovieClip();
			tool		= new MovieClip();
			bAddPower = false;
			bKouxue = false;
			bMenuClick = false;
			money = 0;
			hitCount = 0;
			powerCount = 0;
			curLife = maxLife;
			//从swf中获得子对象并附给UI类中对应的属性
			life = MovieClip(this.getChildByName("life"));
			power = MovieClip(this.getChildByName("power"));
			txtMoney = TextField(this.getChildByName("txtMoney"));
			txtHitCount = TextField(this.getChildByName("txtHitCount"));
			btn = MovieClip(this.getChildByName("btn"));
			tool = MovieClip(this.getChildByName("tool"));trace(tool)
			life.stop();
			power.stop();
			
			updatUI();
		}
		//初始化监听事件，并是按钮盒物品槽隐藏
		private function initListener():void {
			btn.getChildByName("btnFanhui").visible = false;
			btn.getChildByName("btnShengyin").visible = false;
			btn.getChildByName("btnJieshu").visible = false;
			tool.getChildByName("tool1").visible = false;
			tool.getChildByName("tool2").visible = false;
			tool.getChildByName("tool3").visible = false;
			tool.getChildByName("tool4").visible = false;
			
			btn.addEventListener(MouseEvent.MOUSE_OVER, onBtnMouseOver);
			btn.addEventListener(MouseEvent.MOUSE_OUT, onBtnMouseOut);
			tool.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			tool.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}
		public function updatUI():void {
			if (bKouxue == true) {
				updatUiLife();
				bKouxue = false;
			}
			if (bAddPower == true) {
				updatUiPower();
				money += 10;
				txtMoney.text = String(money);
				hitCount ++;
				txtHitCount.text = String("HIT: " + hitCount);
				txtHitCount.visible = true;
			}else {
				hitCount = 0;
				txtHitCount.visible = false;
			}
		}
		
		public function updatUiLife():Boolean {
			curLife --;
			if (curLife == 0) {
				return true;
			}else {
				var frame:int = maxLife - curLife;
				life.gotoAndStop(frame);
				return false;
			}
		}
		
		public function updatUiPower():void {
			powerCount ++;
			if (powerCount == 20) {
				curPower ++;
				powerCount = 0;
			}
			if (curPower == 20) {
				return;
			}else {
				power.gotoAndStop(curPower);
			}
			bAddPower = false;
		}
		
		private function onBtnMouseOver(e:MouseEvent):void {
			btn.getChildByName("btnFanhui").visible = true;
			btn.getChildByName("btnShengyin").visible = true;
			btn.getChildByName("btnJieshu").visible = true;
		}
		private function onBtnMouseOut(e:MouseEvent):void {
			btn.getChildByName("btnFanhui").visible = false;
			btn.getChildByName("btnShengyin").visible = false;
			btn.getChildByName("btnJieshu").visible = false;
		}
		private function onMouseOver(e:MouseEvent):void {
			tool.getChildByName("tool1").visible = true;
			tool.getChildByName("tool2").visible = true;
			tool.getChildByName("tool3").visible = true;
			tool.getChildByName("tool4").visible = true;
		}
		private function onMouseOut(e:MouseEvent):void {
			tool.getChildByName("tool1").visible = false;
			tool.getChildByName("tool2").visible = false;
			tool.getChildByName("tool3").visible = false;
			tool.getChildByName("tool4").visible = false;
		}
		public function resetUiLife():void {
			curLife = maxLife;
		}
		
		public function resetUiPower():void {
			curPower = maxPower;
		}
	}

}